package bdddaosimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import bdd.beans.*;
import bdddaos.*;
import bdddaos.DAOReunion;

public class DAOReunionImpl implements DAOReunion {

	private DBLink dblink;

	public DAOReunionImpl() {		
		super();
		this.dblink = new DBLink(); 
	}
	@Override
	public Reunion save(Reunion reu) {
		try {
			dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("INSERT INTO REUNION (numsalle, libelle, horairedebut, horairefin, iddate)" +
					" VALUES ("+reu.getSalle().getNumsalle()+",'"+reu.getLibelle()+"',"+reu.getHorairedebut()+","+reu.getHorairefin()+","+reu.getJours().getIddate()+")"));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			reu.setNumreunion(outil.findMaxId("REUNION", "NUMREUNION"));
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te");
		}
		finally{
			dblink.deconnexion();
		}
		return reu;
	}

	@Override
	public Reunion find(Integer id) {
		Reunion uneReunion = new Reunion();
		uneReunion.setNumreunion(id);
		
		try {
			dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM REUNION WHERE NUMREUNION = " + id));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				uneReunion.setSalle(new DAOSalleImpl().find(rs.getInt("NUMSALLE")));
				uneReunion.setHorairedebut(rs.getInt("HORAIREDEBUT"));
				uneReunion.setHorairefin(rs.getInt("HORAIREFIN"));
				uneReunion.setJours(new Jours(rs.getDate("IDDATE")));
				uneReunion.setLibelle(rs.getString("LIBELLE"));
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te");
		}
		finally{
			dblink.deconnexion();
		}
		return uneReunion;
	}

	@Override
	public List<Reunion> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Reunion transientObject) {
		// TODO Auto-generated method stub
	}

	@Override
	public void delete(Reunion persistentObject) {
		try {
			dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("DELETE FROM REUNION WHERE NUMREUNION = " + persistentObject.getNumreunion()));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			dblink.deconnexion();
		}
	}
}
